
import Pagination from '@/components/pagination';
import mixin from '@/service/mixin';
import AlertDialog from '@/components/alert-dialog';
import SelectAppList from '@/components/select-app-list';
import UploadFinanceFile from '@/components/upload-finance-file';
import { keyMap } from '@/config/index';

export default {
    name: 'finance-finance-loan-offline-loan',
    components: {
        Pagination,
        AlertDialog,
        SelectAppList,
        UploadFinanceFile,
    },
    mixins: [mixin],
    data() {
        return {
            keyMap,
            form: {
                orderId: '',
                userPhone: '',
                withdrawAccount: '',
                userId: '',
                withdrawChannel: '',
                applyTime: '',
                product: '',
                appName: '',
                userType: '',
            },
            rules: {
                orderId: [],
                userPhone: [],
                withdrawAccount: [],
                userId: [],
                applyTime: [],
            },
            selectedIdList: {
                ids: [],
                orderIds: []
            },
            showOfflineLoan: false, // 显示线下放款
            uploadSuccess: false, // 上传成功
            templateUrl: '/offline-loan.xlsx', // 模板下载地址
        };
    },
    methods: {
        // 获取列表数据
        getTableData(page = this.page) {
            const params = this.fillParams(page);
            this.$api.finance.loan.offlineList(params).then(res => {
                const {status, data, error} = res;
                if ('1' === status && '00000000' === error && data) {
                    this.tableData = data.resultData;
                    this.setPage(data.page, this.page);
                }
                this.tableMaxHeight = document.body.clientHeight - 290;
            });
        },
        // 搜索列表数据
        search() {
            this.page.currentPage = 1;
            this.getTableData();
        },
        // 导出列表数据
        exportExcel() {
            const params = this.fillParams();
            const user = this.$storage.get('user');
            const language = this.$storage.getLocal('language');
            params.language = language;
            params.token = user.token;
            const {$apiConf} = this;
            const url = this.$urlUtil.fillParams(params, $apiConf.baseUrl + $apiConf.urlType.finance.loan.offlineExcel, ['page']);
            this.$openNewBlank(url);
        },
        // 配置查询和导出参数
        fillParams(page = this.page) {
            const form = this.form;
            const params = {
                orderId: '',
                userPhone: '',
                withdrawAccount: '',
                userId: '',
                withdrawChannel: '',
                applyTimeStart: '',
                applyTimeEnd: '',
                product: '',
                appName: '',
                userType: '',
                page: {
                    pageNo: page.currentPage,
                    pageSize: this.page.pageSize
                }
            };
            Object.keys(form).forEach(key => {
                if (typeof form[key] === 'string' && form[key]) {
                    form[key] = form[key].trim();
                    params[key] = form[key];
                }
            });
            if (form.applyTime) {
                params.applyTimeStart = form.applyTime[0];
                params.applyTimeEnd = form.applyTime[1];
            }
            if (form.withdrawChannel instanceof Array) {
                params.withdrawChannel = form.withdrawChannel.join(',');
            }
            return params;
        },
        // 列表多选处理
        handleTableSelect(val) {
            this.selectedIdList.ids = [];
            this.selectedIdList.orderIds = [];
            val.forEach(item => {
                this.selectedIdList.ids.push(item.id);
                this.selectedIdList.orderIds.push(item.orderId);
            });
        },
        // 上传放款文件
        submitUpload(file) {
            const params = new FormData();
            params.append('file', file);
            this.$api.finance.loan.offlineLoad(params).then(res => {
                const { status, data } = res;
                if ('1' === status) {
                    // this.uploadFile = null;
                    const me = this;
                    // 放款成功${data.success}, 放款失败${data.fail}  放款结果
                    this.$alert(this.$t('finance.loanResultDetail', { success: data.success, fail: data.fail }), this.$t('finance.loanResult'), {
                        callback() {
                            me.getTableData(); // 更新列表数据
                        }
                    });
                    this.showOfflineLoan = false;
                }
            });
        },
        // 批量取消
        batchCancel() {
            const ids = this.selectedIdList.ids;
            // 确认批量处理${ids.length}条订单？
            this.$confirm(this.$t('common.confirmBatchHandleTips', { count: ids.length }), this.$t('common.tips')).then(() => {
                const params = {
                    idList: ids,
                    type: '3', // type:触发类型，1-待放款列表，2-放款失败列表, 3-取消线下放款，进入待放款
                };
                this.$api.finance.loan.updateOfflineStatus(params).then(res => {
                    const { status } = res;
                    if ('1' === status) {
                        this.$message.success(this.$t('common.successfulOperation')); // '操作成功'
                        this.getTableData();
                    }
                });
            }).catch(() => {});
        },
    },
    created() {
        this.getTableData();
    }
}
